package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.OrderListDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.OrderList;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service(interfaceClass = OrderListService.class)
@Transactional
public class OrderListServiceImpl implements OrderListService {

    @Autowired
    private OrderListDao orderListDao;
    @Autowired
    private SetmealDao setmealDao;

    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        // 开启分页
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        //调用dao查
        Page<OrderList> all = orderListDao.findAll(queryPageBean.getQueryString());
        return new PageResult(all.getTotal(), all.getResult());
    }

    /**
     * 获取所有的检查套餐
     * @return
     */
    @Override
    public List<Setmeal> findAllSetmeal() {

        return setmealDao.findAll();
    }

    /**
     * 保存一个预约列表
     * @param ids 预约列表关联的套餐id
     * @param orderList 预约列表的基本 信息
     */
    @Override
    public void saveOrderList(Integer[] ids, OrderList orderList) {
        //先保存基本信息
        orderListDao.saveOrderListBasic(orderList);
        // 在保存中间表 关联,准备数据
        HashMap<String, Integer> map = new HashMap<>();
        map.put("orderList_id", orderList.getId());
        for (int i = 0; i < ids.length; i++) {
            map.put("setmeal_id", ids[i]);
            orderListDao.saveOrderListMiddle(map);
        }


    }

    /**
     * 根据id删除orderList
     * @param id 用户的id
     */
    @Override
    public void deleteOrderListById(int id) {
            //先删中间表
        orderListDao.deleteMiddle(id);
        // 在删除基本信息
        orderListDao.deleteBasic(id);
    }

    @Override
    public void deleteOrderLists(int[] ids) {


        for (int i = 0; i < ids.length; i++) {
            //先删中间表
            orderListDao.deleteMiddle(ids[i]);
            // 在删除基本信息
            orderListDao.deleteBasic(ids[i]);
        }
    }
}
